package com.sduept.nwld.fault.outer.model.factory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.entity.FaultReportMain;
import com.sduept.bigdata.fault.entity.FaultReportMainProtection;
import com.sduept.bigdata.fault.entity.FaultReportStationDetail;
import com.sduept.cim.model.core.Equipment;
import com.sduept.cim.model.core.Substation;
import com.sduept.cim.model.protection.ProtectionEquipment;
import com.sduept.cim.model.wires.PowerTransformer;
import com.sduept.nwld.fault.outer.model.FaultTripInfo;
import com.sduept.nwld.fault.outer.model.ProtectInfo;
import com.sduept.nwld.fault.outer.model.StationTripInfo;
import com.sduept.utils.DateUtil;


public class StationDeviceOuterFaultModelFactory extends AbstractOuterFaultModelFactory{
	
	private static AbstractOuterFaultModelFactory factory = new StationDeviceOuterFaultModelFactory();
	private StationDeviceOuterFaultModelFactory(){}
	public static AbstractOuterFaultModelFactory getInstance(){
		return factory;
	}
	/**
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@Override
	public  FaultTripInfo getFaultTripInfo(FaultRecord record)throws Exception{
		List<FaultReportMain> mains  = superDao.findByCondition(FaultReportMain.class, "faultRecord.id=?", record.getId());
		if(mains.size()>0){
			return buildFaultTripInfo(record,mains.get(0));
		}
		return null;
	}
	
	private FaultTripInfo buildFaultTripInfo(FaultRecord record,FaultReportMain main){
		FaultTripInfo result = new FaultTripInfo();
		result.setId(record.getId());
		result.setStatus(record.getStatus());
		Equipment primary=null;
		if("变压器".equals(record.getEtype())){
			primary = cm.getPowerTransformersById(record.getEquipment_id());
			result.setDeviceRdfId(((PowerTransformer)primary).getRdfId());
			result.setDeviceGid(((PowerTransformer)primary).getId());
		}
		result.setFaultDate(record.getFaultTime().getTime());
		result.setFaultPhase(record.getFaultPhase());
		result.setDeviceName(record.getEquipment());
		for(FaultReportStationDetail sdetail: main.getFaultReportStationDetails()){
			StationTripInfo sti  = buildStationTripInfo(record.getFaultTime(),record.getEquipment(), record.getEquipment_id(),sdetail);
			if(StringUtils.isEmpty(sti.getRecolse())){
				result.setRecolse(sti.getRecolse());
			}
			result.getStationTripInfos().add(sti);
		}
		//设置，雷电信息
		String thunder =wdm.getNewFaultThunderInfo(record);
		if(StringUtils.isNotEmpty(thunder)){
			result.setFaultAnlysis(thunder);
		}
		StringBuilder sb = new StringBuilder(DateUtil.getStringDate(new Date(result.getFaultDate()), DateUtil.pattern_ymdhm)+"，"+record.getEquipment());
		if(StringUtils.isNotEmpty(result.getFaultPhase())){
			sb.append("，"+result.getFaultPhase());
		}
		sb.append("跳闸");
		result.setTripName(sb.toString());
		return result;
	}
	/**
	 * 构建变电站跳闸信息
	 * @param smodel
	 * @param pmodel
	 * @return
	 */
	private StationTripInfo buildStationTripInfo(Date faultDate,String deviceName,String deviceId,FaultReportStationDetail sdetail){
		StationTripInfo result = new StationTripInfo();
		if(StringUtils.isNotEmpty(sdetail.getStationId())){
			Substation station =  cm.getStationById(sdetail.getStationId());
			if(station!=null){
				result.setStationRdfId(station.getRdfId());
				result.setStationGid(station.getId());
			}
		}
		result.setStationName(sdetail.getStationName());
		List<ProtectionEquipment> pelist = cm.listMainEquipmentsByStationIdAndprotectedId(sdetail.getStationId(), deviceId);
		List<ProtectInfo> list = buildProtectInfo(pelist,sdetail.getFaultReportMainProtectionActions(),deviceName,faultDate,sdetail.getStationName());
		result.getProtectInfos().addAll(list);
		return result;
	}
	/**
	 * 构造保护动作信息
	 * @param lineId
	 * @param model
	 * @return
	 */
	private List<ProtectInfo> buildProtectInfo(List<ProtectionEquipment> pelist,Collection<FaultReportMainProtection> mains,String deviceName,Date faultTime,String stationName){
		List<ProtectInfo> result = new ArrayList<ProtectInfo>();
		List<ProtectInfo> pilist = getSFProtectReport(faultTime, deviceName, stationName);
		//构造本次保护动作模型
		for(ProtectionEquipment pe:pelist){
			ProtectInfo info = new ProtectInfo();
			info.setProtectName(pe.getType());
			for(ProtectInfo pi:pilist){
				if(pe.getName().equals(pi.getProtectName())){
					info.setComponet(pi.getComponet());
					break;
				}
			}
			for(FaultReportMainProtection main:mains){
				if(!"trip".equals(main.getActionType())){
					continue;
				}
				if(pe.getId().equals(main.getProtectionId())){
					if(StringUtils.isEmpty(info.getPhase())){
						info.setPhase(main.getPhase());
					}else{
						info.setPhase(info.getPhase()+main.getPhase());
					}
				}
			}
			if(StringUtils.isNotEmpty(info.getPhase())){
				char[] str=info.getPhase().toCharArray();
				Arrays.sort(str);
				String phase = new String(str);
				info.setPhase(phase);
			}
			result.add(info);
		}
		return result;
	}
}
